package com.dq.controller.api.message;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.ApiMessageQuery;
import com.dq.vo.Vo;
import com.dq.domain.message.oneself.Message;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.service.AllService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author cf
 */
@Api(description = "小程序消息通用方法")
@RestController
@RequestMapping("/api/wxmessage/")
public class ApiMessageController extends AllService {

    @ApiOperation("消息查看")
    @GetMapping("/look/{id}")
    public Vo updateType(@PathVariable @ApiParam(value = "消息ID") Long id) throws AdminException {

        messageService.look(id, "all");

        return new Vo(HttpStatus.OK);
    }

    /**
     * app/pc通用
     * @param messageQuery
     * @return
     * @throws AdminException
     */
    @ApiOperation("获取顾客所有消息类型列表")
    @PostMapping("/list")
    public Vo list(@ApiParam("messageType ：[all: 所有类型, wxSystem: 系统通知, wxService: 服务通知, wxSubscribe: 预约通知]") @RequestBody ApiMessageQuery messageQuery) throws AdminException {
        if(messageQuery.getMessageType() == null) {
            throw new AdminException("请选择消息类型");
        }

        SysUser user = sysUserService.getUser();

        messageQuery.format();
        Map<String, Object> query = messageQuery.parseMap();
        String theme = null;
        if(StringUtils.isNotBlank(messageQuery.getTheme())) {
            theme = "%" + messageQuery.getTheme() + "%";
        }

        List<Object> returnJson = new ArrayList<>();

        List<Integer> messageTypes = getIntegers(messageQuery.getMessageType().name);
        List<Message> messageList = messageService.messageListSqlApi(theme, messageTypes, user.getId(), messageQuery.getBeginSearchTime(), messageQuery.getEndSearchTime(), messageQuery.getPage(), messageQuery.getSize());
        Long count = messageService.messageCountSqlApi(theme, messageTypes, user.getId(), messageQuery.getBeginSearchTime(), messageQuery.getEndSearchTime());
        for (Message message : messageList) {
            returnJson.add(message.toVO());
        }

        PageHelper.startPage(messageQuery.getPage(), messageQuery.getSize());
        PageInfo pageInfo = new PageInfo(returnJson);
        pageInfo.setTotal(count);

        return new Vo(pageInfo);
    }

    public List<Integer> getIntegers(String messageTypeName) {
        List<Integer> messageTypes = new ArrayList<>();
        Message.MessageType[] messageTypeList = Message.MessageType.values();

        if(messageTypeName.equals(Message.MessageType.all.name)) {
            // todo no
            for (Message.MessageType messageType : messageTypeList) {
                messageTypes.add(messageType.ordinal());
            }
        }else {
            for (Message.MessageType messageType : messageTypeList) {
                if(messageTypeName.equals(messageType.name)) {
                    messageTypes.add(messageType.ordinal());
                    break;
                }
            }
        }
        return messageTypes;
    }
}
